package com.yf.document.modules.flowable.model.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.boot.base.api.api.dto.PagingReqDTO;
import com.yf.boot.base.api.utils.BeanMapper;
import com.yf.document.modules.flowable.model.dto.AcdeModelDTO;
import com.yf.document.modules.flowable.model.entity.AcdeModel;
import com.yf.document.modules.flowable.model.mapper.AcdeModelMapper;
import com.yf.document.modules.flowable.model.service.AcdeModelService;
import org.json.JSONObject;
import org.json.XML;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
* <p>
* 业务实现类
* </p>
*
* @author Panjp
* @since 2022-11-04 10:34
*/
@Service
public class AcdeModelServiceImpl extends ServiceImpl<AcdeModelMapper, AcdeModel> implements AcdeModelService {

    @Override
    public IPage<AcdeModelDTO> paging(PagingReqDTO<AcdeModelDTO> reqDTO) {
        return  baseMapper.paging(reqDTO.toPage(), reqDTO.getParams());
    }


    @Override
    public void save(AcdeModelDTO reqDTO){
        AcdeModelDTO dto = this.detail(reqDTO.getId());
        if(null != dto){
            reqDTO.setVersion(dto.getVersion()+1);
        }else{
            reqDTO.setUpdateTime(new Date());
            reqDTO.setVersion(1);
        }
        JSONObject xmlJSONObj = XML.toJSONObject(reqDTO.getThumbnail());
        reqDTO.setModelEditorJson(xmlJSONObj.toString());
        //复制参数
        AcdeModel entity = new AcdeModel();
        BeanMapper.copy(reqDTO, entity);
        this.saveOrUpdate(entity);

    }

    @Override
    public void delete(List<String> ids){
        //批量删除
        this.removeByIds(ids);
    }

    @Override
    public AcdeModelDTO detail(String id){
        AcdeModelDTO entity = baseMapper.getById(id);
        return entity;
    }

    @Override
    public List<AcdeModelDTO> list(AcdeModelDTO reqDTO){

        //分页查询并转换
        QueryWrapper<AcdeModel> wrapper = new QueryWrapper<>();

        //转换并返回
        List<AcdeModel> list = this.list(wrapper);

        //转换数据
        List<AcdeModelDTO> dtoList = BeanMapper.mapList(list, AcdeModelDTO.class);

        return dtoList;
    }

}
